package model

import (
	"gorm.io/gorm"
	"travel-app/common/initialize"
	"travel-app/common/proto/user"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:varchar(32);not null;index:ip_idx"`
	Password string `gorm:"type:varchar(32);not null;index:ip_idx"`
}

func (u User) GetUserMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Where("mobile=?", mobile).Take(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

func (u User) CreateUser(in *user.RegisterReq) (user *User, err error) {
	user = &User{
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	err = initialize.DB.Create(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

type UserReal struct {
	gorm.Model
	Name string `gorm:"type:varchar(50);not null;index:ip_idx"`
	Card string `gorm:"type:varchar(50);not null;index:ip_idx"`
}

func (r UserReal) CreateUser(in *user.UserRealReq) (user *UserReal, err error) {
	user = &UserReal{
		Name: in.Name,
		Card: in.Card,
	}
	err = initialize.DB.Create(user).Error
	if err != nil {
		return nil, err
	}
	return
}

func (r UserReal) FindUser() (user []*UserReal, err error) {
	err = initialize.DB.Find(&user).Error
	if err != nil {
		return nil, err
	}
	return
}
